Imports System.Drawing

Namespace TressleWorks.Modules.ThermoChart

    Public Class FontSettings

        Private sSettingString As String
        Private sFontName As String
        Private sFontSize As String
        Private sFontColor As String
        Private sFontColor2 As String
        Private sFontColor3 As String

        Private iFontColorCount As Integer

        Private bVisible As Boolean
        Private bBold As Boolean

        Private _Color As Color
        Private _Color2 As Color
        Private _Color3 As Color

        Private _Size As Unit

        Public Sub New()
            Call Initialize()
        End Sub

        Public Sub New(ByVal psFontSetting As String)
            Call Initialize()
            Call Parse(psFontSetting)
        End Sub

        Private Sub Initialize()
            'format: name [,size [,color[:color2[:color3]] [,bold]]]]
            sSettingString = "Arial, 10pt, Black"
            
            sFontName = String.Empty
            sFontSize = String.Empty
            sFontColor = String.Empty
            sFontColor2 = String.Empty
            sFontColor3 = String.Empty

            bBold = False
            bVisible = True
        End Sub

        Public ReadOnly Property SettingString() As String
            Get
                Return sSettingString
            End Get
        End Property

        Public Property FontName() As String
            Get
                Return sFontName
            End Get
            Set(ByVal value As String)
                sFontName = value
            End Set
        End Property

        Public Property FontSize() As String
            Get
                Return sFontSize
            End Get
            Set(ByVal value As String)
                sFontSize = value.Trim
                _Size = Unit.Parse(sFontSize)
            End Set
        End Property

        Public Property FontColor() As String
            Get
                Return sFontColor
            End Get
            Set(ByVal value As String)
                sFontColor = value.Trim
                _Color = ToColorFromString(sFontColor)
            End Set
        End Property

        Public Property FontColor2() As String
            Get
                Return sFontColor2
            End Get
            Set(ByVal value As String)
                sFontColor2 = value.Trim
                _Color2 = ToColorFromString(sFontColor2)
            End Set
        End Property

        Public Property FontColor3() As String
            Get
                Return sFontColor3
            End Get
            Set(ByVal value As String)
                sFontColor3 = value.Trim
                _Color3 = ToColorFromString(sFontColor3)
            End Set
        End Property

        Public ReadOnly Property FontColorCount() As Integer
            Get
                Return iFontColorCount
            End Get
        End Property

        Public Property Bold() As Boolean
            Get
                Return bBold
            End Get
            Set(ByVal value As Boolean)
                bBold = value
            End Set
        End Property

        Public Property Visible() As Boolean
            Get
                Return bVisible
            End Get
            Set(ByVal value As Boolean)
                bVisible = value
            End Set
        End Property

        'Public Property DisplayAngle() As Integer
        '    Get
        '        Return iDisplayAngle
        '    End Get
        '    Set(ByVal value As Integer)
        '        iDisplayAngle = Val(value)
        '    End Set
        'End Property

        Public ReadOnly Property Color() As Color
            Get
                Return _Color
            End Get
        End Property

        Public ReadOnly Property Size() As Unit
            Get
                Return _Size
            End Get

        End Property

        Public Sub Parse(ByVal psFontSettings As String)

            FontName = "Arial"
            FontColor = "Black"
            FontColor2 = String.Empty
            FontColor3 = String.Empty
            iFontColorCount = 1
            FontSize = "10pt"
            'DisplayAngle = 0
            Bold = False
            Visible = True


            If psFontSettings.Length = 0 Then
                Exit Sub
            End If

            If psFontSettings.StartsWith(";") Then
                Exit Sub
            End If

            'save it away
            sSettingString = psFontSettings

            Dim sSections() As String

            sSections = Split(psFontSettings, ",")

            'format: name [,size [,color[:color2[:color3]] [,bold]]]]

            'fontname
            FontName = sSections(0)

            'size
            If sSections.Length > 1 Then
                FontSize = sSections(1)
                If FontSize.Length = 0 Then
                    FontSize = "10pt"
                End If
            Else
                FontSize = "10pt"
            End If

            'color
            If sSections.Length > 2 Then
                If sSections(2).Length > 0 Then
                    If sSections(2).Contains(":") Then
                        Dim sColors() As String = sSections(2).Split(":")
                        If sColors.Length > 0 Then
                            FontColor = sColors(0).Trim
                            iFontColorCount = 1
                            If sColors.Length > 1 Then
                                FontColor2 = sColors(1).Trim
                                iFontColorCount = 2
                                If sColors.Length > 2 Then
                                    FontColor3 = sColors(2).Trim
                                    iFontColorCount = 3
                                End If
                            End If
                        End If
                    Else
                        FontColor = sSections(2)
                        iFontColorCount = 1
                    End If
                End If
            End If

            'bold
            If sSections.Length > 3 Then
                If sSections(3).ToUpper.Trim = "BOLD" Then
                    bBold = True
                End If
            End If

            ''visible: true, false
            'If sSections.Length > 4 Then
            '    If Not Boolean.TryParse(sSections(4), bVisible) Then
            '        bVisible = True  '' default to true
            '    End If
            'End If

        End Sub

        Public Function AsFont() As Font
            Dim eFontStyle As FontStyle = FontStyle.Regular
            If bBold Then
                eFontStyle = FontStyle.Bold
            End If
            Dim oFont As New Font(FontName, CSng(Size.Value), eFontStyle, GraphicsUnit.Point)

            Return oFont
        End Function


        Private Function ToColorFromString(ByVal psColorString As String) As Color

            If psColorString.Length = 0 Then
                Return Color.White
            End If

            If psColorString.StartsWith("&H") Then
                Dim Num As Integer = Convert.ToInt32(psColorString, 16)
                Dim R As Integer = Num \ &H10000
                Dim G As Integer = (Num Mod &H100) \ &H100
                Dim B As Integer = Num Mod &H100
                Return Color.FromArgb(R, G, B)
            End If

            If psColorString.StartsWith("#") Then
                Return ColorTranslator.FromHtml(psColorString)
            End If

            Return Color.FromName(psColorString.Trim)

        End Function


    End Class

End Namespace


